﻿using System;
using System.Collections.Generic;
using System.Windows.Media;

namespace Daq.Utils
{

    public class DistinctColorGenerator
    {
        private readonly Random _random;
        private readonly List<double> _usedHues;
        private const double MinSaturation = 0.7; // 最低饱和度
        private const double MinBrightness = 0.7; // 最低亮度

        public DistinctColorGenerator(int? seed = null)
        {
            _random = seed.HasValue ? new Random(seed.Value) : new Random();
            _usedHues = new List<double>();
        }

        // 生成指定数量的高区分度颜色
        public List<Color> GenerateColors(int count)
        {
            var colors = new List<Color>();

            // 初始尝试均匀分布色调
            double hueStep = 360.0 / count;

            for (int i = 0; i < count; i++)
            {
                double hue;
                int attempts = 0;

                do
                {
                    // 先尝试均匀分布的色调
                    if (attempts < 3)
                    {
                        hue = (i * hueStep + _random.NextDouble() * 15) % 360;
                    }
                    // 如果冲突太多，尝试完全随机的色调
                    else
                    {
                        hue = _random.NextDouble() * 360;
                    }

                    attempts++;

                } while (!IsDistinctHue(hue) && attempts < 10);

                // 记录使用过的色调
                _usedHues.Add(hue);

                // 随机饱和度和亮度，但保持在较高水平以确保鲜艳
                double saturation = MinSaturation + _random.NextDouble() * (1 - MinSaturation);
                double brightness = MinBrightness + _random.NextDouble() * (1 - MinBrightness);

                colors.Add(HsvToRgb(hue, saturation, brightness));
            }

            return colors;
        }

        // 检查新色调是否与已使用的色调有足够差异
        private bool IsDistinctHue(double hue)
        {
            foreach (var usedHue in _usedHues)
            {
                // 计算色调差异（考虑环形特性）
                double hueDiff = Math.Min(Math.Abs(hue - usedHue), 360 - Math.Abs(hue - usedHue));

                // 差异小于阈值则认为不够明显
                if (hueDiff < 30) // 30度是经验阈值，可根据需要调整
                {
                    return false;
                }
            }

            return true;
        }

        // HSV转RGB算法
        private Color HsvToRgb(double hue, double saturation, double value)
        {
            double r = 0, g = 0, b = 0;

            if (saturation == 0)
            {
                r = g = b = value;
            }
            else
            {
                // 将色调转换到0-6区间
                double h = hue / 60.0;
                int sector = (int)Math.Floor(h);
                double fractional = h - sector;

                double p = value * (1 - saturation);
                double q = value * (1 - saturation * fractional);
                double t = value * (1 - saturation * (1 - fractional));

                switch (sector)
                {
                    case 0: r = value; g = t; b = p; break;
                    case 1: r = q; g = value; b = p; break;
                    case 2: r = p; g = value; b = t; break;
                    case 3: r = p; g = q; b = value; break;
                    case 4: r = t; g = p; b = value; break;
                    case 5: r = value; g = p; b = q; break;
                }
            }

            return Color.FromRgb(
                (byte)Math.Round(r * 255),
                (byte)Math.Round(g * 255),
                (byte)Math.Round(b * 255)
            );
        }

        // 重置已使用的色调记录
        public void Reset()
        {
            _usedHues.Clear();
        }
    }
    class ColorGenerator
    {
        private static Random random = new Random();

        // 生成随机颜色
        public static DaqColor GenerateRandomColor()
        {
            byte r = (byte)random.Next(0, 256);
            byte g = (byte)random.Next(0, 256);
            byte b = (byte)random.Next(0, 256);
            return new DaqColor(r, g, b);
        }

        // 生成随机颜色
        public static (byte, byte, byte) GenerateRandomColorRgb()
        {
            int r = random.Next(0, 256);
            int g = random.Next(0, 256);
            int b = random.Next(0, 256);
            return ((byte)r, (byte)g, (byte)b);
        }

        // 生成具有一定亮度范围的随机颜色
        public static DaqColor GenerateRandomColorWithBrightness(float minBrightness, float maxBrightness)
        {
            float brightness = (float)random.NextDouble() * (maxBrightness - minBrightness) + minBrightness;
            float hue = (float)random.NextDouble();
            float saturation = (float)random.NextDouble();

            // 将HSL转换为RGB
            var rgb = HslToRgb(hue, saturation, brightness);
            return new DaqColor((byte)rgb.Item1, (byte)rgb.Item2, (byte)rgb.Item3);
        }

        public static List<DaqColor>? GenerateRandomColors(int count)
        {
            if (count < 1) return null;
            // 创建生成器实例
            var generator = new DistinctColorGenerator();

            // 生成10个高区分度颜色
            var colors = generator.GenerateColors(10);
            List<DaqColor> daqColors = [];
            foreach(var color in colors)
            {
                daqColors.Add(new DaqColor(color.R, color.G, color.B, color.A));
            }
            return daqColors;
        }

        //// 生成具有一定亮度范围的随机颜色
        //public static (byte, byte, byte) GenerateRandomColorWithBrightnessRgb(float minBrightness, float maxBrightness)
        //{
        //    float brightness = (float)random.NextDouble() * (maxBrightness - minBrightness) + minBrightness;
        //    float hue = (float)random.NextDouble();
        //    float saturation = (float)random.NextDouble();

        //    // 将HSL转换为RGB
        //    var rgb = HslToRgb(hue, saturation, brightness);
        //    return ((byte)rgb.Item1, (byte)rgb.Item2, (byte)rgb.Item3);
        //}

        //// 生成具有一定亮度范围的随机颜色
        //public static DaqColor[] GenerateRandomColorWithBrightness(float minBrightness, float maxBrightness, int count)
        //{
        //    DaqColor[] colors = new DaqColor[count];
        //    for (int i = 0; i < count; i++)
        //    {
        //        colors[i] = GenerateRandomColorWithBrightness(minBrightness, maxBrightness);
        //    }
        //    return colors;
        //}

        // HSL到RGB的转换函数
        private static Tuple<float, float, float> HslToRgb(float h, float s, float l)
        {
            if (s == 0f)
            {
                return Tuple.Create(l * 255, l * 255, l * 255);
            }

            float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
            float p = 2 * l - q;
            float r = HueToRgb(p, q, h + 1f / 3f);
            float g = HueToRgb(p, q, h);
            float b = HueToRgb(p, q, h - 1f / 3f);

            return Tuple.Create(r * 255, g * 255, b * 255);
        }

        // HUE到RGB的转换函数
        private static float HueToRgb(float p, float q, float t)
        {
            if (t < 0) t += 1;
            if (t > 1) t -= 1;
            if (t < 1f / 6f) return p + (q - p) * 6 * t;
            if (t < 1f / 2f) return q;
            if (t < 2f / 3f) return p + (q - p) * (2f / 3f - t) * 6;
            return p;
        }
    }
}
